<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="600" :title="`${props.title}基础表格数据`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="props.isView"
      :model="props.row"
      :hide-required-asterisk="props.isView"
    >
      <el-form-item label="用户头像" prop="avatar">
        <UploadImage v-model:path="props.row!.avatar" v-model:url="props.row!.avatarUrl" :file-size="3">
          <template #empty>
            <el-icon><el-icon-avatar /></el-icon>
            <span>请上传头像</span>
          </template>
          <template #tip>头像大小不能超过 10M</template>
        </UploadImage>
      </el-form-item>
      <!-- <el-form-item label="用户照片" prop="photo">
        <UploadMultipleImage v-model:file-list="drawerProps.row!.photo">
          <template #empty>
            <el-icon><el-icon-picture /></el-icon>
            <span>请上传照片</span>
          </template>
          <template #tip>照片大小不能超过 10M</template>
        </UploadMultipleImage>
      </el-form-item> -->
      <el-form-item label="用户姓名" prop="username">
        <el-input v-model="props.row!.name" placeholder="请填写用户姓名" clearable></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="props.row!.gender" placeholder="请选择性别" clearable>
          <el-option v-for="item in genderType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="props.row!.mobile" placeholder="请填写手机号码" clearable></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input v-model="props.row!.idCard" placeholder="请填写身份证号" clearable></el-input>
      </el-form-item>
      <el-form-item label="居住地址" prop="address">
        <el-input v-model="props.row!.address" placeholder="请填写居住地址" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch v-model="localStatus"></el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!props.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="TableBaseDrawer">
import { ref, reactive, watchEffect } from 'vue'
import { genderType } from '@/utils/dict'
import { ElMessage, FormInstance } from 'element-plus'
import { ITableDemo } from '@/api/tableDemo/type'
import UploadImage from '@/components/Upload/index.vue'
// import UploadMultipleImage from '@/components/Upload/multiple.vue'

const rules = reactive({
  name: [{ required: true, message: '请填写用户姓名' }],
  gender: [{ required: true, message: '请选择性别' }],
  mobile: [{ required: true, message: '请填写手机号码' }],
  idCard: [{ required: true, message: '请填写身份证号' }],
  avatar: [{ required: true, message: '请上传用户头像' }],
  photo: [{ required: true, message: '请上传用户照片' }],
  email: [{ required: true, message: '请填写邮箱' }],
  address: [{ required: true, message: '请填写居住地址' }]
})

interface DrawerProps {
  title: string
  isView: boolean
  row: Partial<ITableDemo.BaseModel>
  api?: (params: any) => Promise<any>
  getTableList?: () => void
}

const drawerVisible = ref(false)
const props = ref<DrawerProps>({
  isView: false,
  title: '',
  row: {}
})
// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  props.value = params
  drawerVisible.value = true
}

// 使用计算属性返回 computedStatus 的值，这样可以保持响应式
const localStatus = ref(props.value.row.status ?? true)

watchEffect(() => {
  props.value.row.status = localStatus ? 1 : 0
})

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>()
const handleSubmit = () => {
  ruleFormRef.value!.validate(async (valid) => {
    if (!valid) return
    try {
      await props.value.api!(props.value.row)
      ElMessage.success({ message: `${props.value.title}用户成功！` })
      props.value.getTableList!()
      drawerVisible.value = false
    } catch (error) {
      console.log(error)
    }
  })
}

defineExpose({
  acceptParams
})
</script>
